package com.heatup.apt.management.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.common.constants.CourseConstants.CourseType;
import com.heatup.apt.common.constants.DataDictConstants;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.management.service.DataDictService;
import com.heatup.apt.management.service.ProductServiceDetailService;
import com.heatup.apt.management.service.coach.CoachService;
import com.heatup.apt.management.service.course.CourseSecretService;
import com.heatup.apt.management.service.course.CourseService;
import com.heatup.apt.management.service.course.CourseStandardService;
import com.heatup.apt.management.service.store.StoreService;
import com.heatup.apt.model.api.course.CourseSecretModel;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.course.CourseStandard;
import com.heatup.apt.model.management.SysUser;


/**
 * Created by b3st9u on 16/8/22.
 */
@Controller
@RequestMapping("system/course")
public class CourseSecretController extends BaseController {
	@Autowired
	CourseService courseService;
	@Autowired
	CourseSecretService courseSecretService;
	@Autowired
	CoachService coachService;
	@Autowired
	StoreService storeService;
	@Autowired
	CourseStandardService courseStandardService;
    @Autowired
    DataDictService dataDictService;
    @Autowired
    ProductServiceDetailService productServiceDetailService;


	/**
	 * 删除私教
	 * 
	 * @param courseId 课程 ID
	 * @param response
	 */
	@RequestMapping(value="/delete_course_secret")
	public void deleteCourseSecret(Integer courseId, HttpServletResponse response){
		String msg;
		if(courseSecretService.deleteCourseSercet(courseId)){
			msg="success";
		}else{
			msg="false";
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg",msg);
		responseSendMsg(response, jsonObject.toString());
	}
	/**
	 * 保存私教课程信息
	 *
	 */
	@RequestMapping(value="/course_sercet_save")
    @ResponseBody
	public String saveCourseSecret(CourseSecretModel courseSecretModel){
		SysUser user = this.getUserInfo();
		int userId = user.getId();
		courseSecretModel.setStatus("2");
		courseSecretModel.setCreatedBy(userId);
		courseSecretModel.setCreatedTime(new Date());
		courseSecretModel.setCoachCount(1);
		courseSecretModel.setCourseType(CourseType.private_course.getType());
		if(courseSecretService.addCourseSecretInfo(courseSecretModel)){
			return "success";
		}else{
			return "false";
		}
	}
	
	 @RequestMapping(value = "/to_course_secret", method = {RequestMethod.GET, RequestMethod.POST})
	    public ModelAndView toCourseCamp(){
	        ModelAndView modelAndView = new ModelAndView("course/course-secret-list");
	        modelAndView.addObject("coachList", coachService.queryAllCoachInfo());
	        return modelAndView;
	    }

    /**
     * 私教新增页面
     * @return
     */
    @RequestMapping(value = "/to_add_course_sercet")
    public ModelAndView addCourseSecret() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("coachList", coachService.queryAllCoachInfo());
        modelAndView.addObject("storeList", JsonUtils.obj2Json(storeService.queryAllOnlineStore()));
        modelAndView.addObject("dataDictList", JsonUtils.obj2Json(dataDictService.queryDataDicts(DataDictConstants.DICT_TYPE_COURSE_ATTR)));
        modelAndView.addObject("courseStandards", courseStandardService.queryAllCourseStandard());
        modelAndView.addObject("productServiceDetailLists", productServiceDetailService.queryProductServiceDetailList());
        modelAndView.setViewName("course/course-secret-add");
        return modelAndView;
    }

    /**
     * 私教课程编辑页面
     * @return
     */
    @RequestMapping(value = "/to_course_secret_edit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView editSecretCourse(@RequestParam int id) {
        ModelAndView modelAndView = new ModelAndView("course/course-secret-edit");
        modelAndView.addObject("coachList", coachService.queryAllCoachInfo());
        modelAndView.addObject("storeList", JsonUtils.obj2Json(storeService.queryAllOnlineStore()));
        List<CourseStandard> list = courseStandardService.queryAllCourseStandard();
		modelAndView.addObject("dataDictList", JsonUtils.obj2Json(dataDictService.queryDataDicts(DataDictConstants.DICT_TYPE_COURSE_ATTR)));
        modelAndView.addObject("courseStandards", list);        
        CourseSecretModel c = courseSecretService.getCourseSecretModelById(id);
        modelAndView.addObject("course",c);
        modelAndView.addObject("courseJson", JsonUtils.obj2Json(c));
        modelAndView.addObject("productServiceDetailLists", productServiceDetailService.queryProductServiceDetailList());
        return modelAndView;
    }
    
    
    /**
	 * 更新私教课程信息
	 *
	 */
	@RequestMapping(value = "/course_sercet_update")
	@ResponseBody
	public String updateCourseSercet(CourseSecretModel courseSecretModel) {
		if (courseSecretModel == null) {
			return "false";
		}
		int i = courseService.queryCourseOrderByCourseId(courseSecretModel.getId());
		if (i > 0) {
			CourseSecretModel old = courseSecretService.getCourseSecretModelById(courseSecretModel.getId());
			if (old != null) {
//				私教课程如果有相关的正常订单，则不能修改价格和课程数量
				if (old.getServicePrice().getPrimePrice().compareTo(courseSecretModel.getServicePrice().getPrimePrice())!=0) {
					return  "该私教课程已存在预定的订单，不能修改价格信息.";
				}
			}
		}

		SysUser user = this.getUserInfo();
		int userId = user.getId();
		courseSecretModel.setStatus("2");
		courseSecretModel.setUpdatedBy(userId);
		courseSecretModel.setUpdatedTime(new Date());
		courseSecretModel.setCoachCount(1);
		courseSecretModel.setCourseType(CourseType.private_course.getType());
		if (courseSecretService.updateCourseSecretInfo(courseSecretModel)) {
			return "success";
		} else {
			return "false";
		}
	}


    /** 
     * 查询私教课程列表
     */
    @RequestMapping(value = "/course_sercet_list")
	public void courseSercetList(HttpServletRequest request, HttpServletResponse response) {
		int pageIndex = RequestTools.RequestInt(request, "pageIndex", 0);
		int pageSize = RequestTools.RequestInt(request, "pageSize", 0);
		String coachId = RequestTools.RequestString(request, "coachId", "");
		String status = RequestTools.RequestString(request, "status", "");
		String id = RequestTools.RequestString(request, "id", null);
		if(coachId.equals("-1")){
			coachId=null;
		}
		if(status.equals("-1")){
			status=null;
		}

		if (StringUtil.isEmpty(pageIndex)) {
			pageIndex = 0;
		}
		if (StringUtil.isEmpty(pageSize)) {
			pageSize = 10;
		}
		int page = pageIndex;
		if (page > 0) {
			page = page - 1;
		}
		pageIndex=page*pageSize;
		List<CoachInfo> coachInfoList = coachService.queryAllCoachInfo();
		Map<String, Object> map = courseSecretService.findCourseSercetList(pageIndex, pageSize, coachId, status,id);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("course_sercet_list", map.get("course_sercet_list"));
		jsonObject.put("course_sercet_count", map.get("course_sercet_count"));
		jsonObject.put("coachList",coachInfoList);
		responseSendMsg(response, jsonObject.toString());
	}


	
}
